//
//  GWYHomeSatuesTableViewCell.swift
//  swift-GWYWeiBo
//
//  Created by x on 2017/4/17.
//  Copyright © 2017年 HLB. All rights reserved.
//  

/*
    三大特性:  继承  多态  封装
 */

import UIKit

let WIDTH = UIScreen.main.bounds.size.width

/// 定义一个枚举保存cell的重用表示
///
/// - NormalCell: <#NormalCell description#>
/// - ForwardCell: <#ForwardCell description#>
enum GWYHomeSatuesTableViewCellIdentifier: String {
    case NormalCell = "NormalCell"
    case ForwardCell = "ForwardCell"
    
    static func cellID(model: Status) -> String {
        
        //swift中的枚举中是可以定义方法的
        //调用枚举的rawValue 是返回枚举的原始值  (ForwardCell.rawValue -> "ForwardCell")
        //在枚举中使用static修饰一个方法 相当于在类中使用class修饰方法
        return model.retweeted_status != nil ? ForwardCell.rawValue : NormalCell.rawValue
    }
}

class GWYHomeSatuesTableViewCell: UITableViewCell {
    
    /// 保存配图的宽度约束
    var pictureWidthCons: NSLayoutConstraint?
    /// 保存配图的高度约束
    var pictureHeightCons: NSLayoutConstraint?
    /// 保存配图的顶部约束
    var pictureTopCons: NSLayoutConstraint?
    
    
    /// 数据模型
    var modle:Status? {
        didSet {
            //设置顶部视图的数据
            topView.modle = modle
            //设置正文
            u_wb_contentLable.text = modle?.text
            
            //设置配图的相应属性
            //设置完约束之后要赋值 (如果有转发则赋值转发的model 没有转发则赋值原创的model)
            pictureView.modle = modle?.retweeted_status != nil ? modle?.retweeted_status : modle
            //1.根据模型计算配图尺寸 (注意计算尺寸需要用到我们的一个数据模型)
            let size = pictureView.calculateImageSize() //得到配图的尺寸
            //2设置配图的尺寸
            pictureWidthCons?.constant = size.width
            pictureHeightCons?.constant = size.height
            pictureTopCons?.constant = size.height == 0 ? 0 :10
        }
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //初始化子控件
        setupUI()
    }
    //初始化UI
    func setupUI() -> Void {
        selectionStyle = UITableViewCellSelectionStyle.none
        //1.添加子控件
        contentView.addSubview(topView)
        contentView.addSubview(u_wb_contentLable)
        contentView.addSubview(pictureView)
        contentView.addSubview(footView)
        //2.布局子控件
        //给topView添加约束
        contentView.addConstraint(NSLayoutConstraint.init(item: topView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: contentView, attribute: NSLayoutAttribute.top, multiplier: 1.0, constant: 0))
        contentView.addConstraint(NSLayoutConstraint.init(item: topView, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: contentView, attribute: NSLayoutAttribute.left, multiplier: 1.0, constant: 0))
        topView.addConstraint(NSLayoutConstraint.init(item: topView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: WIDTH))
        topView.addConstraint(NSLayoutConstraint.init(item: topView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: topView.topViewHeight()))
        //2.7 微博正文添加约束
        contentView.addConstraint(NSLayoutConstraint.init(item: u_wb_contentLable, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: topView, attribute: NSLayoutAttribute.left, multiplier: 1.0, constant: 10))
        contentView.addConstraint(NSLayoutConstraint.init(item: u_wb_contentLable, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: topView, attribute: NSLayoutAttribute.bottom, multiplier: 1.0, constant: 10))
        
        //2.8给配图添加约束
        pictureWidthCons = NSLayoutConstraint.init(item: pictureView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 0)
        pictureHeightCons = NSLayoutConstraint.init(item: pictureView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 0)
        pictureView.addConstraint(pictureWidthCons!)
        pictureView.addConstraint(pictureHeightCons!)

        //2.9工具栏添加约束
        footView.addConstraint(NSLayoutConstraint.init(item: footView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 44))
        footView.addConstraint(NSLayoutConstraint.init(item: footView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: WIDTH))
        contentView.addConstraint(NSLayoutConstraint.init(item: footView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: pictureView, attribute: NSLayoutAttribute.bottom, multiplier: 1.0, constant: 10))
        contentView.addConstraint(NSLayoutConstraint.init(item: footView, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: pictureView, attribute: NSLayoutAttribute.left, multiplier: 1.0, constant: -10))
    }
    func rowHeight(model: Status) -> CGFloat {
        //1.为了调用model的didSet方法 重新布局
        self.modle = model
        //2.布局之后强制更新UI
        self.layoutIfNeeded()
        //返回行高
        //footView中最打的Y值就是行高
        return footView.frame.maxY;
    }
    //设置
    //MARK: -- 懒加载  
    
    /// 顶部视图
    private lazy var topView: StatuesTableViewTopView = StatuesTableViewTopView()
    ///微博正文
    lazy var u_wb_contentLable = { ()-> UILabel in
        let tt = UILabel.creatLbale(color: UIColor.darkGray, fontSize: 15)
        tt.numberOfLines = 0
        //设置lable的的约束最大宽度
        tt.preferredMaxLayoutWidth = WIDTH - 20
        return tt
    }()
    
    /// 配图
    lazy var pictureView:StatuesPictureView = StatuesPictureView()
    ///微博工具栏
    lazy var footView:StatuesFootView = StatuesFootView()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
